package com.linguamathematica.oa4j;

import java.util.Set;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static com.linguamathematica.oa4j.Base.newSet;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.linguamathematica.oa4j.Analysis.Decisiveness;
import com.linguamathematica.oa4j.Analysis.OfferingGuidance;
import com.linguamathematica.oa4j.Analysis.Polarity;
import com.linguamathematica.oa4j.Analysis.RequestingGuidance;
import com.linguamathematica.oa4j.Analysis.Temporality;
import com.linguamathematica.oa4j.DemographicsAnalysis.Age;
import com.linguamathematica.oa4j.DemographicsAnalysis.Education;
import com.linguamathematica.oa4j.DemographicsAnalysis.Gender;
import com.linguamathematica.oa4j.DemographicsAnalysis.Language;
import com.linguamathematica.oa4j.StylesAnalysis.Contrast;
import com.linguamathematica.oa4j.StylesAnalysis.Flamboyance;
import com.linguamathematica.oa4j.StylesAnalysis.Slang;
import com.linguamathematica.oa4j.TopicsAnalysis.Domain;
import com.linguamathematica.oa4j.TopicsAnalysis.Noun;

public class AnalysisServiceTestGivenTextB extends AbstractAnalysisServiceTest
{
	private static String textB;

	@Test
	public void returns_CorrectActionsAnalysis() throws Exception
	{
		final ActionsAnalysis expected = actionsAnalysis(action(topic("set up", 3), of(Decisiveness.LOW, 1),
				of(OfferingGuidance.NOT_AT_ALL, 1), of(RequestingGuidance.NOT_AT_ALL, 1), type("create", 1),
				of(Temporality.PAST, 1)));

		assertThat(actionsAnalysisOf(textB), is(as(expected)));
	}

	@Test
	public void returns_CorrectDemographicsAnalysis() throws Exception
	{
		final DemographicsAnalysis expected = demographics(of(Age.ADULT, 0), of(Gender.NEUTRAL, 0),
				of(Education.PRE_SECONDARY, 1), of(Language.ENGLISH, 20.38));

		assertThat(demographicAnalysisOf(textB), is(as(expected)));
	}

	@Test
	public void returns_CorrectDomains() throws Exception
	{
		final Set<Domain> expected = newSet();

		assertThat(domainsIn(textB), are(as(expected)));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void returns_CorrectLocations() throws Exception
	{
		final Set<Analysis.Result<String>> expected = newSet(location("UK", 7), location("Helmand", 4),
				location("Kandahar", 4));

		assertThat(locationsIn(textB), are(as(expected)));
	}

	@Test
	public void returns_CorrectProperNouns() throws Exception
	{
		mockResponseFor(textB, "OAResponseToTestText_B.xml");

		final Set<Noun> expected = newSet(//@formatter:off

				properNoun(topic("UK", 7), of(Polarity.NEGATIVE, -.39), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "country")),

				properNoun(topic("Helmand", 4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "province")),

				properNoun(topic("Kandahar", 4), of(Polarity.NEGATIVE, -.2), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "city")));//@formatter:on

		assertThat(properNounsIn(textB), are(as(expected)));
	}

	@Test
	public void returns_CorrectStyleAnalysis() throws Exception
	{
		final StylesAnalysis expected = style(of(Polarity.NEGATIVE, -.54), of(Polarity.NEGATIVE, -.54),
				of(Polarity.NEGATIVE, -.54), of(OfferingGuidance.NOT_AT_ALL, 1), of(RequestingGuidance.NOT_AT_ALL, 1),
				of(Decisiveness.LOW, 1), of(Slang.NO_SLANG, 0), of(Flamboyance.SOMEWHAT_FLAMBOYANT, 3),
				of(Contrast.NOT_AT_ALL, 0), of(Temporality.PAST, 1));

		assertThat(styleAnalysisOf(textB), is(as(expected)));
	}

	@Test
	public void returns_CorrectTopTopics() throws Exception
	{
		final Set<Noun> expected = newSet(//@formatter:off

				topTopic(topic("UK", 7), of(Polarity.NEGATIVE, -.39), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "country")),

				topTopic(topic("base", 7), of(Polarity.NEGATIVE, -.39), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1)),

				topTopic(topic("Helmand", 4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "province")),

				topTopic(topic("Kandahar", 4), of(Polarity.NEGATIVE, -.2), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "city")),

				topTopic(topic("province", 2), of(Polarity.NEGATIVE, -.54), of(OfferingGuidance.NOT_AT_ALL, 1),
						of(RequestingGuidance.NOT_AT_ALL, 1)));//@formatter:on

		assertThat(topTopicsIn(textB), are(as(expected)));
	}

	@Before
	public void setUpEveryTime() throws Exception
	{
		httpService = mock(HTTPService.class);
		mockResponseFor(textB, "OAResponseToTestText_B.xml");
		service = new DefaultAnalysisService(key, httpService);
	}

	@BeforeClass
	public static void setUpOnce() throws Exception
	{
		textB = loadFrom("TestText_B.txt");
	}
}
